package com.dimong.loan.sdk.utils;

import android.view.View;
import android.view.ViewGroup;

public class ViewEnabledUtils {

    /**
     * 防连点时间
     */
    public static final long DELAY_TIME = 1000;

    public static void setEnabledViewGroup(final ViewGroup parent, long delayMillis) {
        if (parent == null) {
            return;
        }
        setEnabledView(parent, delayMillis);
        if (parent.getChildCount() > 0) {
            for (int i = 0; i < parent.getChildCount(); ++i) {
                final View child = parent.getChildAt(i);
                if (child instanceof ViewGroup) {
                    setEnabledViewGroup((ViewGroup) child, delayMillis);
                } else {
                    setEnabledView(child, delayMillis);
                }
            }
        }
    }

    public static void setEnabledView(final View v) {
        setEnabledView(v, DELAY_TIME);
    }

    public static void setEnabledView(final View v, long delayMillis) {
        if (v == null) {
            return;
        }
        v.setEnabled(false);
        v.postDelayed(new Runnable() {
            @Override
            public void run() {
                v.setEnabled(true);
            }
        }, delayMillis);
    }

    /**
     * 设置view防止连点
     *
     * @param v
     * @param delayMillis
     * @param exception   例外规则,view id集合
     */
    public static void setEnabledView(final View v, long delayMillis, int... exception) {
        if (v == null) {
            return;
        }
        if (exception != null && exception.length > 0) {
            for (int id : exception) {
                if (v.getId() == id) {
                    return;
                }
            }
        }
        setEnabledView(v, delayMillis);
    }

}
